package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.gulimall.member.exception.PhoneExistsException;
import com.atguigu.gulimall.member.exception.UsernameExistsException;
import com.atguigu.gulimall.member.feign.CouponFeignService;
import com.atguigu.gulimall.member.vo.LoginUserVo;
import com.atguigu.gulimall.member.vo.RegisterUserVo;
import com.atguigu.gulimall.member.vo.SocialUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author xiaolin
 * @email 956704533@qq.com
 * @date 2020-06-09 09:03:22
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    CouponFeignService couponFeignService;

    @GetMapping("/getMembers")
    public R getMembers(){
        MemberEntity memberEntity=new MemberEntity();
        memberEntity.setUsername("小林");
        R memberCoupons = couponFeignService.getMemberCoupons();
        return new R().put("member",memberEntity).put("coupon",Arrays.asList(memberCoupons));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据手机号、用户名注册用户
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterUserVo vo){
        try{
            memberService.register(vo);
        }catch (UsernameExistsException e){
            return R.error(BizCodeEnume.USERNAME_EXISTS.getCode(),BizCodeEnume.USERNAME_EXISTS.getMsg());
        }catch (PhoneExistsException e){
            return R.error(BizCodeEnume.PHONE_EXISTS.getCode(),BizCodeEnume.PHONE_EXISTS.getMsg());
        }
        return R.ok();
    };

    /**
     * 普通登录
     * @param vo
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody LoginUserVo vo){
       MemberEntity memberEntity=memberService.login(vo);
       if(memberEntity != null){
           return R.ok().setData(memberEntity);
       }else {
           return R.error(BizCodeEnume.USERACCOUNT_VALID.getCode(),BizCodeEnume.USERACCOUNT_VALID.getMsg());
       }
    }

    /**
     * 社交登录
     * @param vo
     * @return
     */
    @PostMapping("oauth/login")
    public R login(@RequestBody SocialUserVo vo){
        MemberEntity memberEntity=memberService.login(vo);
        if(memberEntity !=null){
            return R.ok().setData(memberEntity);
        }else {
            return R.error(BizCodeEnume.SOCIALUSERACCOUNT_VALID.getCode(),BizCodeEnume.SOCIALUSERACCOUNT_VALID.getMsg());
        }

    }


}
